/**
 */
package com.dingpeikeji.quickdeer.core.modules.exchg.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.DateUtils;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.bus.entity.TCompBus;
import com.dingpeikeji.quickdeer.core.modules.bus.service.TCompBusService;
import com.dingpeikeji.quickdeer.core.modules.exchg.entity.TBusExchg;
import com.dingpeikeji.quickdeer.core.modules.exchg.service.TBusExchgService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.service.OfficeService;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 车辆调拨Controller
 * 
 * @author Zhaoy
 * @version 2016-06-29
 */
@Controller
@RequestMapping(value = "${adminPath}/exchg/tBusExchg")
public class TBusExchgController extends BaseController {

	@Autowired
	private TBusExchgService tBusExchgService;
	@Autowired
	private TCompBusService tCompBusService;
	@Autowired
	private OfficeService officeService;

	@ModelAttribute
	public TBusExchg get(@RequestParam(required = false) String id) {
		if (StringUtils.isNotBlank(id)) {
			return tBusExchgService.get(id);
		} else {
			return new TBusExchg();
		}
	}

	@RequiresPermissions("exchg:tBusExchg:view")
	@RequestMapping(value = { "list", "" })
	public String list(TBusExchg tBusExchg, HttpServletRequest request, HttpServletResponse response, Model model) {
		TCompBus tCompBus = new TCompBus();
		tCompBus.setOffice(UserUtils.getUser().getOffice());
		Page<TCompBus> page = tCompBusService.findPage(new Page<TCompBus>(request, response), tCompBus);
		model.addAttribute("page", page);
		return "modules/exchg/tBusExchgList";
	}

	@RequiresPermissions("exchg:tBusExchg:view")
	@RequestMapping(value = { "outlist" })
	public String outlist(TBusExchg tBusExchg, HttpServletRequest request, HttpServletResponse response, Model model) {
		tBusExchg.setSourcecompany(UserUtils.getUser().getOffice());
		Page<TBusExchg> page = tBusExchgService.findPage(new Page<TBusExchg>(request, response), tBusExchg);
		model.addAttribute("page", page);
		return "modules/exchg/tBusOutList";
	}

	@RequiresPermissions("exchg:tBusExchg:view")
	@RequestMapping(value = { "inlist" })
	public String inlist(TBusExchg tBusExchg, HttpServletRequest request, HttpServletResponse response, Model model) {
		tBusExchg.setDestcompany(UserUtils.getUser().getOffice());
		Page<TBusExchg> page = tBusExchgService.findPage(new Page<TBusExchg>(request, response), tBusExchg);
		model.addAttribute("page", page);
		return "modules/exchg/tBusInList";
	}

	@SuppressWarnings("deprecation")
	@RequiresPermissions("exchg:tBusExchg:view")
	@RequestMapping(value = "form")
	public String form(TBusExchg tBusExchg, Model model, HttpServletRequest request) {
		// 前台起始日期的默认值为明天
		String busid = request.getParameter("busid");
		if (busid != null && !busid.equals("")) {
			Date tomorrow = new Date();
			tomorrow.setDate(tomorrow.getDate() + 1);
			model.addAttribute("tomorrow", DateUtils.formatDate(tomorrow, "yyyy-MM-dd"));
		
			TCompBus tmpbus = new TCompBus();
			tmpbus = tCompBusService.get(busid);
			tBusExchg.setCompbus(tmpbus);

			// 借出公司为当前用户的公司
			tBusExchg.setSourcecompany(UserUtils.getUser().getOffice());

			// 读取所有客运公司
			List<Map<String, Object>> mapList = Lists.newArrayList();
			Office qryOffice = new Office();
			qryOffice.setFlatType(Office.FLAT_TYPE_BUS);
			List<Office> list = officeService.findListByTerm(qryOffice);
			for (int i = 0; i < list.size(); i++) {
				Office e = list.get(i);
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("pIds", e.getParentIds());
				map.put("name", e.getName());
				mapList.add(map);
			}

			model.addAttribute("officelist", mapList);
			model.addAttribute("tBusExchg", tBusExchg);
		}
		return "modules/exchg/tBusExchgForm";
	}

	@RequiresPermissions("exchg:tBusExchg:edit")
	@RequestMapping(value = "save")
	public String save(TBusExchg tBusExchg, Model model, RedirectAttributes redirectAttributes,
			HttpServletRequest request) {
		if (!beanValidator(model, tBusExchg)) {
			return form(tBusExchg, model, request);
		}

		TCompBus tCompBus = tCompBusService.get(tBusExchg.getCompbus().getBusid());
		tBusExchg.setCompbus(tCompBus);

		// 设置借出公司id
		tBusExchg.setSourcecompany(tCompBus.getOffice());

		// 设置操作人id
		User user = new User();
		user.setId(UserUtils.getUser().getId());
		tBusExchg.setCreateBy(user);
		tBusExchgService.save(tBusExchg);

		// 对巴士信息进行修改
		tCompBus.setOffice(tBusExchg.getDestcompany());
		tCompBus.setStartdate(tBusExchg.getStartdate());
		tCompBus.setEnddate(tBusExchg.getEnddate());
		tCompBusService.update(tCompBus);
		addMessage(redirectAttributes, "车辆借调成功");
		return "redirect:" + Global.getAdminPath() + "/exchg/tBusExchg/?repage";
	}

	@RequiresPermissions("exchg:tBusExchg:edit")
	@RequestMapping(value = "delete")
	public String delete(TBusExchg tBusExchg, RedirectAttributes redirectAttributes) {
		tBusExchgService.delete(tBusExchg);
		addMessage(redirectAttributes, "删除车辆调拨成功");
		return "redirect:" + Global.getAdminPath() + "/exchg/tBusExchg/?repage";
	}

}
